package com.gt.web.srv.mapstructs;

import cn.hutool.core.codec.Base64;
import com.gt.web.srv.domain.SsUser;
import com.gt.web.srv.utils.JwtUtils;
import com.gt.web.srv.web.api.request.UserRequest;
import com.gt.web.srv.web.api.resource.ResponseUserVo;
import com.gt.web.srv.web.api.resource.dto.TokenDto;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.factory.Mappers;

/**
 * @Author jh
 * @Date 2024/5/18 16:01
 * @PackageName:com.gt.web.srv.converter
 * @ClassName: AdvertisementConverter
 * @Description: TODO
 */
@Mapper
public interface SsUserStruct {

    SsUserStruct INSTANCE = Mappers.getMapper(SsUserStruct.class);

    SsUser simple(UserRequest cmd);

    /**
     * 构建用户信息
     * @param resultPeopleRepDto
     * @param token
     * @return
     */
    default ResponseUserVo toResponseUserVo(SsUser resultPeopleRepDto) {
        ResponseUserVo userVo = new ResponseUserVo();
        userVo.setUserId(resultPeopleRepDto.getUserId());
        userVo.setUserName(resultPeopleRepDto.getName());
        userVo.setUnionId(resultPeopleRepDto.getUnionId());
        userVo.setOpenId(resultPeopleRepDto.getOpenId());
        userVo.setUserStatus(resultPeopleRepDto.getStatus());
        userVo.setUserStatus(resultPeopleRepDto.getStatus());
        if (StringUtils.isBlank(resultPeopleRepDto.getOpenId())) {
            String tmpOpenId = "tmp_"+resultPeopleRepDto.getUserId();
            String encode = Base64.encode(tmpOpenId);
            userVo.setOpenId(encode);
            resultPeopleRepDto.setOpenId(encode);
        }
        String token = JwtUtils.createToken(TokenDto.builder()
                .openId(resultPeopleRepDto.getOpenId())
                .userId(resultPeopleRepDto.getUserId())
                .build());
        userVo.setToken(token);
        return userVo;
    }
}
